﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LDSP.BRL;

namespace LDSP.UI.Admin
{
    public partial class AddCase : BRL.BaseAdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    this.CheckQS();
                }
                if (this.RB())
                {
                    if (!IsPostBack)
                    {
                        this.InitDataSelect();
                    }
                }
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

        protected Category m_category = null;

        protected int LastID
        {
            get { return this.GetVSInt("LastID"); }
            set
            {
                this.SetVS("LastID", value);
                m_category = null;
            }
        }
        protected Category Category
        {
            get
            {
                if (m_category == null)
                {
                    m_category = new Category();
                    if (LastID != 0)
                    {
                        m_category.GetProperties(LastID);
                    }
                }
                return m_category;
            }
        }
        protected bool IsAdd
        {
            get { return LastID == 0; }
        }

        protected void CheckQS()
        {
            LastID = this.GetInt(this.Request.QueryString["id"]);
            if (Category.ID == 0)
            {
                LastID = 0;
            }
        }
        protected bool RB()
        {
            return this.AllowSystemUser();
        }
        protected void SetStatus()
        {
            this.lbl_op.Text = IsAdd ? "【添加】" : "【修改】";
            this.lbl_op.Style["color"] = IsAdd ? "red" : "blue";
            this.ClientComfirm(this.btn_ok, string.Format("您确定执行{0}操作吗？", IsAdd ? "添加" : "修改"));
        }
        protected void InitDataSelect()
        {
            this.txt_name.Text = Category.Name;
            this.txt_comment.Text = Category.Comment;
            this.BindDDLDocType();
            this.SetStatus();
        }
        protected void BindDDLDocType()
        {
            this.ddl_docType.Items.Clear();
            this.ddl_docType.Items.Add("生产事故");
            this.ddl_docType.Items.Add("环保事故");
        }

        protected void ValidateDataBefore()
        {
        }
        protected void ValidateDataAfter()
        {
            if (Category.Name == "")
            {
                throw new BYFW.BOL.CustomException("名称不能为空！");
            }
        }
        protected void SetData()
        {
            this.ValidateDataBefore();
            Category.Name = this.txt_name.Text.Trim();
            Category.Comment = this.txt_comment.Text;
            Category.DocType = (DocType)this.GetInt(this.ddl_docType.SelectedValue);
            this.ValidateDataAfter();
        }
        protected void Add()
        {
            this.SetData();
            //Category.Add();
            this.LastID = 0;
            this.JSAlert("添加成功！");
        }
        protected void Update()
        {
            this.SetData();
            //Category.Update();
            this.JSAlert("更新成功！");
        }

        protected void btn_add_Click(object sender, EventArgs e)
        {
            try
            {
                this.LastID = 0;
                this.InitDataSelect();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void btn_ok_Click(object sender, EventArgs e)
        {
            try
            {
                if (IsAdd)
                {
                    this.Add();
                }
                else
                {
                    this.Update();
                }
                this.InitDataSelect();

            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
    }
}